package com.huawei123.hasee.cniao5store.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.utils.AppActivityManagerUtils;
import com.huawei123.hasee.cniao5store.utils.LayoutUtils;
import com.huawei123.hasee.cniao5store.utils.LogUtils;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.zhxu.library.RxRetrofitApp;

import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：Activity基类
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 上午 9:52
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 上午 9:52
 * 修改备注：
 */

public abstract class BaseActivity extends RxAppCompatActivity {


    protected ViewGroup mRL_bar_layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
        AppActivityManagerUtils.getInstance().addActivity(this);//添加Activity到栈中统一管理
        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        /*加载布局文件*/
        initLayout();
        ButterKnife.bind(this);
        initView();
    }

    /**
     * 设置沉浸式状态栏
     */
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mRL_bar_layout = (ViewGroup) findViewById(R.id.mRL_bar_layout);
            final int statusHeight = LayoutUtils.getStatusBarHeight(this);
            mRL_bar_layout.post(new Runnable() {
                @Override
                public void run() {
                    int titleHeight = mRL_bar_layout.getHeight();
                    android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)
                            mRL_bar_layout.getLayoutParams();
                    params.height = statusHeight + titleHeight;
                    mRL_bar_layout.setLayoutParams(params);
                }
            });
        }
    }


    /**
     * 添加Activity转场动画
     *
     * @param intent
     */
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);
    }

    /**
     * 添加Activity转场动画
     *
     * @param intent
     * @param requestCode
     */
    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_left);
    }

    protected void openActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * 添加退出Activity转场动画
     */
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_right);
    }

    @Override
    protected void onDestroy() {
        AppActivityManagerUtils.getInstance().removeActivity(this);//关闭时移除Activity
        super.onDestroy();
    }

    protected abstract void initLayout();

    protected abstract void initView();

    protected void initData() {
    }

    public void showToast(String mag) {
        LogUtils.showToast(mag);
    }
}
